package Presentation.GraphicUI;

import BusinessLogic.ControllerFacade;
import Domain.Exceptions.UserNotLoggedInException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class MainScreen extends JFrame {

    public  static final String MAIN_MENU                = "MAIN_MENU";
    private static final String NO_USER_PANEL            = "NO_USER_PANEL";
    private static final String MAIN_PANEL               = "MAIN_PANEL";
    public  static final String REGISTER_PANEL           = "REGISTER_PANEL";
    public  static final String LOGIN_PANEL              = "LOGIN_PANEL";
    public  static final String ADD_MOVIE_PANEL          = "ADD_MOVIE_PANEL";
    public  static final String SEARCH_MOVIE_PANEL       = "SEARCH_MOVIE_PANEL";
    public  static final String GET_RECOMMENDATION_PANEL = "GET_RECOMMENDATION_PANEL";
    public  static final String ADD_RECOMMENDATION_PANEL = "ADD_RECOMMENDATION_PANEL";
    public  static final String EXIT_PROGRAM             = "EXIT_PROGRAM";

    private String m_activePanel;
    private JPanel m_panels;
    private HashMap<String,MovieSystemPanel> m_panelMap;

    private JButton m_exitButton;
    private JButton m_backButton;

    public ControllerFacade m_controllerFacade;

    public MainScreen(ControllerFacade m_controllerFacade) {
        super("Main Screen");
        this.m_controllerFacade = m_controllerFacade;
        
        JFrame.setDefaultLookAndFeelDecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel m_mainPanel = new JPanel(new BorderLayout(0, 0));
        m_mainPanel.setBackground(Color.YELLOW);

        //TOP
        Font f = new Font("Arial",Font.BOLD,20);
        JLabel welcomMsg = new JLabel("Movie System", JLabel.CENTER);
        welcomMsg.setFont(f);
        m_mainPanel.add(welcomMsg, BorderLayout.NORTH);

        //BOTTOM
        JPanel m_bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 80, 0));
        m_exitButton = new JButton("Exit");
        m_backButton = new JButton("Back");
        m_bottomPanel.add(m_exitButton);
        JPanel sign = new JPanel(new GridLayout(2,1));
        sign.add(new JLabel("All Rights Reserved", JLabel.CENTER));
        sign.add(new JLabel("Grupi's MovieSystem", JLabel.CENTER));
        m_bottomPanel.add(sign, BorderLayout.NORTH);
        m_bottomPanel.add(m_backButton);
        m_mainPanel.add(m_bottomPanel, BorderLayout.SOUTH);

        // CENTER       
        m_panelMap = new HashMap<String, MovieSystemPanel>();
        m_panels = new JPanel(new CardLayout());
        init();
        m_mainPanel.add(m_panels, BorderLayout.CENTER);

        add(m_mainPanel);


    }

    public void init(){

        m_panelMap.put(NO_USER_PANEL, new NoUserScreen(this));
        m_panelMap.put(LOGIN_PANEL, new MyLogOnScreen(this));
        m_panelMap.put(REGISTER_PANEL, new AddNewUserScreen(this));
        m_panelMap.put(GET_RECOMMENDATION_PANEL, new ListRecommendationsPanel(this));
        m_panelMap.put(ADD_RECOMMENDATION_PANEL, new AddNewRecommendationScreen(this));
        m_panelMap.put(ADD_MOVIE_PANEL, new AddNewMovieScreen(this));
        //m_panelMap.put(SEARCH_MOVIE_PANEL, new AddNewMovieScreen(this));
        m_panelMap.put(MAIN_PANEL, new MainPanel(this));

        for (Map.Entry<String,MovieSystemPanel> e : m_panelMap.entrySet()){
            m_panels.add(e.getKey(),e.getValue());
        }
    }

    public void run(){

        setActivePanel(MAIN_MENU);

        m_exitButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        m_backButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setActivePanel(MainScreen.MAIN_MENU);
            }
        });

        pack();
        setResizable(false);        
        setLocationRelativeTo(null);
        setVisible(true);
    }


    public void setActivePanel(String panel) {
        String target = panel;
        if (MAIN_MENU.equals(target)){
            try {
                m_controllerFacade.getCurrentUser();
                target = MAIN_PANEL;
            } catch (UserNotLoggedInException e) {
                target = NO_USER_PANEL;
            }
            m_backButton.setEnabled(false);
        }
        else
            m_backButton.setEnabled(true);
        CardLayout cl = (CardLayout)(m_panels.getLayout());
        cl.show(m_panels, target);
        m_activePanel = target;
        if (m_panelMap.containsKey(m_activePanel))
            m_panelMap.get(m_activePanel).setUp();
    }
}
